package org.example.service.pc;

import cn.hutool.core.util.IdUtil;
import org.example.R.R;
import org.example.entity.dto.AddAppealCollectDto;
import org.example.entity.po.AppealCollectPO;
import org.example.entity.vo.admin.AppealCollectVO;
import org.example.entity.vo.pc.PCAppealCollectVO;
import org.example.mapper.AppealCollectMapper;
import org.example.utils.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class AppealCollectService {

    @Autowired
    private AppealCollectMapper appealCollectMapper;

    public R add(AddAppealCollectDto dto) {

        AppealCollectPO po = new AppealCollectPO();
        BeanUtils.copyProperties(dto, po);
        po.setId(IdUtil.getSnowflakeNextId());
        po.setCreateTime(TimeUtils.getCurrentDate());
        po.setDelFlag(0);
        po.setIsOver(0);

        appealCollectMapper.insert(po);
        return R.success("提交诉求成功!");
    }

    public R delete(Long id) {
        appealCollectMapper.delete(id);
        return R.success("删除成功!");
    }

    public R over(Long id) {
        appealCollectMapper.overTask(id);
        return R.success("提交解决!");
    }

    public R adminList(){
        List<AppealCollectVO> list = appealCollectMapper.list();
        list.sort(Comparator.comparing(AppealCollectVO::getCreateTime).reversed());
        return R.success("获取成功!").setData(list);
    }

    public R pcList() {
        List<PCAppealCollectVO> resultList = new ArrayList<>();
        List<AppealCollectVO> list = appealCollectMapper.list();
        list.forEach(obj->{
            PCAppealCollectVO vo = new PCAppealCollectVO();
            BeanUtils.copyProperties(obj, vo);
            if (obj.getIsOver() == 0) {
                vo.setIsOver("未解决");
            } else {
                vo.setIsOver("已解决");
            }
            resultList.add(vo);
        });
        resultList.sort(Comparator.comparing(PCAppealCollectVO::getCreateTime).reversed());
        return R.success("获取成功!").setData(resultList);
    }
}
